using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace SchEdu.Framework.DataModel
{
    public class DependencyList : List<SchEduDataSet.DependencyRow>
    {
        public DependencyList()
            : base()
        {
        }

        public DependencyList(DependencyList dependencies)
            : base(dependencies)
        {
        }

        public DependencyList(SchEduDataSet.DependencyRow[] dependencies)
            :base(dependencies)
        {
        }

        public DependencyList(SchEduDataSet.DependencyDataTable dependencies)
        {
            foreach (SchEduDataSet.DependencyRow dependency in dependencies)
            {
                Add(dependency);
            }
        }

        public void RemoveOrder(SchEduDataSet.OrderRow order)
        {
            DependencyList dependencyToBeRemoved = new DependencyList();
            foreach (SchEduDataSet.DependencyRow dependency in this)
            {
                if (dependency.SuccessorOrderID == order.ID || dependency.PredecessorOrderID == order.ID)
                {
                    dependencyToBeRemoved.Add(dependency);
                }
            }
            foreach(SchEduDataSet.DependencyRow dependency in dependencyToBeRemoved)
            {
                this.Remove(dependency);
            }
        }

        public bool HasSuccessor(SchEduDataSet.OrderRow order)
        {
            foreach (SchEduDataSet.DependencyRow dependency in this)
            {
                if (dependency.PredecessorOrderID == order.ID)
                {
                    return true;
                }
            }
            return false;
        }

        public bool HasPredecessor(SchEduDataSet.OrderRow order)
        {
            foreach (SchEduDataSet.DependencyRow dependency in this)
            {
                if (dependency.SuccessorOrderID == order.ID)
                {
                    return true;
                }
            }
            return false;
        }

        public DependencyList GetSuccessor(SchEduDataSet.OrderRow order)
        {
            DependencyList sucessors = new DependencyList();
            foreach (SchEduDataSet.DependencyRow dependency in this)
            {
                if (dependency.PredecessorOrderID == order.ID)
                {
                    sucessors.Add(dependency);
                }
            }
            return sucessors;
        }

        public DependencyList GetPredecessor(SchEduDataSet.OrderRow order)
        {
            DependencyList predecessors = new DependencyList();
            foreach (SchEduDataSet.DependencyRow dependency in this)
            {
                if (dependency.SuccessorOrderID == order.ID)
                {
                    predecessors.Add(dependency);
                }
            }
            return predecessors;
        }

    }
}
